// 审核通过后给用户发送小程序通知,接收一个_id
//a_auditing用到
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
        console.log('event:', event)
        const message = await db
                .collection('message')
                .where({
                        user_sid: event.id,
                        status: false
                })
                .get();

        console.log('message:', message.data)
        
        if(message.data.length>0){
                try {
                        // 发送订阅消息  
                                
                        await cloud.openapi.subscribeMessage.send({
                          touser: message.data[0]._openid,//接收者的openid
                          page: '/pages/userShow/userShow?id='+message.data[0].user_id,
                          data: message.data[0].data,
                          templateId: message.data[0].templateId,
                        });
                        // 发送成功后将消息的状态改为已发送
                        await db
                          .collection('message')
                          .doc(message.data[0].user_sid)
                          .update({
                            data: {
                              status: true,
                            },
                          });
                      } catch (e) {
                       console.log('发送消息错误:',e)
                      }
        }else{
               console.log('用户未订阅')
        }
        
}